//
//  PlayVideoViewController.m
//  iEPD
//
//  Created by xavi on 1/7/13.
//  Copyright (c) 2013 Mobioneer Ltd. All rights reserved.
//

#import "PlayVideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface PlayVideoViewController ()<MPMediaPickerControllerDelegate,UIActionSheetDelegate>
{
    NSURL *_url;
    MPMoviePlayerController *moviePlayerController;
}
@end

@implementation PlayVideoViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAction)];
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [paths objectAtIndex:0] ;
    path = [path stringByAppendingPathComponent:@"Demo.m4v"];
    NSLog(@"%@",path);
    
    _url = [NSURL fileURLWithPath:path];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path])		//Does file exist?
    {
        moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:_url];
        [moviePlayerController prepareToPlay];
        [moviePlayerController.view setFrame:self.view.bounds];
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.movieSourceType=MPMovieSourceTypeFile;
        moviePlayerController.scalingMode = MPMovieScalingModeNone;
        [moviePlayerController play];
        //UIImage *singleFrameImage = [moviePlayerController thumbnailImageAtTime:10 timeOption:MPMovieTimeOptionExact];
    } else NSLog(@"File does not exist"); 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [moviePlayerController stop];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}

- (void)showAction
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"SAVE_VIDEO", nil), nil];
    [sheet showInView:[[UIApplication sharedApplication] keyWindow]];
}
- (void)saveVideo:(NSURL *)videoURL {
    
    NSLog(@"saving movie at: %@", videoURL);
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:videoURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){}
         ];
    } else NSLog(@"Can not save");
    
}
- (IBAction)onTap
{
    [[self navigationController] setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self saveVideo:_url];
    }
}
@end
